home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / distutils / command / bdist_wininst.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  10.7 KB  |  266 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """distutils.command.bdist_wininst
  5.  
  6. Implements the Distutils 'bdist_wininst' command: create a windows installer
  7. exe-program."""
  8. __revision__ = '$Id: bdist_wininst.py 71422 2009-04-09 22:48:19Z tarek.ziade $'
  9. import sys
  10. import os
  11. import string
  12. from distutils.core import Command
  13. from distutils.util import get_platform
  14. from distutils.dir_util import create_tree, remove_tree
  15. from distutils.errors import *
  16. from distutils.sysconfig import get_python_version
  17. from distutils import log
  18.  
  19. class bdist_wininst(Command):
  20.     description = 'create an executable installer for MS Windows'
  21.     user_options = [
  22.         ('bdist-dir=', None, 'temporary directory for creating the distribution'),
  23.         ('plat-name=', 'p', 'platform name to embed in generated filenames (default: %s)' % get_platform()),
  24.         ('keep-temp', 'k', 'keep the pseudo-installation tree around after ' + 'creating the distribution archive'),
  25.         ('target-version=', None, 'require a specific python version' + ' on the target system'),
  26.         ('no-target-compile', 'c', 'do not compile .py to .pyc on the target system'),
  27.         ('no-target-optimize', 'o', 'do not compile .py to .pyo (optimized)on the target system'),
  28.         ('dist-dir=', 'd', 'directory to put final built distributions in'),
  29.         ('bitmap=', 'b', 'bitmap to use for the installer instead of python-powered logo'),
  30.         ('title=', 't', 'title to display on the installer background instead of default'),
  31.         ('skip-build', None, 'skip rebuilding everything (for testing/debugging)'),
  32.         ('install-script=', None, 'basename of installation script to be run afterinstallation or before deinstallation'),
  33.         ('pre-install-script=', None, 'Fully qualified filename of a script to be run before any files are installed.  This script need not be in the distribution'),
  34.         ('user-access-control=', None, "specify Vista's UAC handling - 'none'/default=no handling, 'auto'=use UAC if target Python installed for all users, 'force'=always use UAC")]
  35.     boolean_options = [
  36.         'keep-temp',
  37.         'no-target-compile',
  38.         'no-target-optimize',
  39.         'skip-build']
  40.     
  41.     def initialize_options(self):
  42.         self.bdist_dir = None
  43.         self.plat_name = None
  44.         self.keep_temp = 0
  45.         self.no_target_compile = 0
  46.         self.no_target_optimize = 0
  47.         self.target_version = None
  48.         self.dist_dir = None
  49.         self.bitmap = None
  50.         self.title = None
  51.         self.skip_build = 0
  52.         self.install_script = None
  53.         self.pre_install_script = None
  54.         self.user_access_control = None
  55.  
  56.     
  57.     def finalize_options(self):
  58.         if self.bdist_dir is None:
  59.             if self.skip_build and self.plat_name:
  60.                 bdist = self.distribution.get_command_obj('bdist')
  61.                 bdist.plat_name = self.plat_name
  62.             
  63.             bdist_base = self.get_finalized_command('bdist').bdist_base
  64.             self.bdist_dir = os.path.join(bdist_base, 'wininst')
  65.         
  66.         if not self.target_version:
  67.             self.target_version = ''
  68.         
  69.         if not (self.skip_build) and self.distribution.has_ext_modules():
  70.             short_version = get_python_version()
  71.             if self.target_version and self.target_version != short_version:
  72.                 raise DistutilsOptionError, "target version can only be %s, or the '--skip_build' option must be specified" % (short_version,)
  73.             self.target_version != short_version
  74.             self.target_version = short_version
  75.         
  76.         self.set_undefined_options('bdist', ('dist_dir', 'dist_dir'), ('plat_name', 'plat_name'))
  77.         if self.install_script:
  78.             for script in self.distribution.scripts:
  79.                 if self.install_script == os.path.basename(script):
  80.                     break
  81.                     continue
  82.             else:
  83.                 raise DistutilsOptionError, "install_script '%s' not found in scripts" % self.install_script
  84.  
  85.     
  86.     def run(self):
  87.         if sys.platform != 'win32':
  88.             if self.distribution.has_ext_modules() or self.distribution.has_c_libraries():
  89.                 raise DistutilsPlatformError('distribution contains extensions and/or C libraries; must be compiled on a Windows 32 platform')
  90.         self.distribution.has_c_libraries()
  91.         if not self.skip_build:
  92.             self.run_command('build')
  93.         
  94.         install = self.reinitialize_command('install', reinit_subcommands = 1)
  95.         install.root = self.bdist_dir
  96.         install.skip_build = self.skip_build
  97.         install.warn_dir = 0
  98.         install.plat_name = self.plat_name
  99.         install_lib = self.reinitialize_command('install_lib')
  100.         install_lib.compile = 0
  101.         install_lib.optimize = 0
  102.         if self.distribution.has_ext_modules():
  103.             target_version = self.target_version
  104.             if not target_version:
  105.                 if not self.skip_build:
  106.                     raise AssertionError, 'Should have already checked this'
  107.                 target_version = sys.version[0:3]
  108.             
  109.             plat_specifier = '.%s-%s' % (self.plat_name, target_version)
  110.             build = self.get_finalized_command('build')
  111.             build.build_lib = os.path.join(build.build_base, 'lib' + plat_specifier)
  112.         
  113.         for key in ('purelib', 'platlib', 'headers', 'scripts', 'data'):
  114.             value = string.upper(key)
  115.             if key == 'headers':
  116.                 value = value + '/Include/$dist_name'
  117.             
  118.             setattr(install, 'install_' + key, value)
  119.         
  120.         log.info('installing to %s', self.bdist_dir)
  121.         install.ensure_finalized()
  122.         sys.path.insert(0, os.path.join(self.bdist_dir, 'PURELIB'))
  123.         install.run()
  124.         del sys.path[0]
  125.         mktemp = mktemp
  126.         import tempfile
  127.         archive_basename = mktemp()
  128.         fullname = self.distribution.get_fullname()
  129.         arcname = self.make_archive(archive_basename, 'zip', root_dir = self.bdist_dir)
  130.         self.create_exe(arcname, fullname, self.bitmap)
  131.         if self.distribution.has_ext_modules():
  132.             pyversion = get_python_version()
  133.         else:
  134.             pyversion = 'any'
  135.         self.distribution.dist_files.append(('bdist_wininst', pyversion, self.get_installer_filename(fullname)))
  136.         log.debug("removing temporary file '%s'", arcname)
  137.         os.remove(arcname)
  138.         if not self.keep_temp:
  139.             remove_tree(self.bdist_dir, dry_run = self.dry_run)
  140.         
  141.  
  142.     
  143.     def get_inidata(self):
  144.         lines = []
  145.         metadata = self.distribution.metadata
  146.         lines.append('[metadata]')
  147.         if not metadata.long_description:
  148.             pass
  149.         info = '' + '\n'
  150.         
  151.         def escape(s):
  152.             return string.replace(s, '\n', '\\n')
  153.  
  154.         for name in [
  155.             'author',
  156.             'author_email',
  157.             'description',
  158.             'maintainer',
  159.             'maintainer_email',
  160.             'name',
  161.             'url',
  162.             'version']:
  163.             data = getattr(metadata, name, '')
  164.             if data:
  165.                 info = info + '\n    %s: %s' % (string.capitalize(name), escape(data))
  166.                 lines.append('%s=%s' % (name, escape(data)))
  167.                 continue
  168.         
  169.         lines.append('\n[Setup]')
  170.         if self.install_script:
  171.             lines.append('install_script=%s' % self.install_script)
  172.         
  173.         lines.append('info=%s' % escape(info))
  174.         lines.append('target_compile=%d' % (not (self.no_target_compile)))
  175.         lines.append('target_optimize=%d' % (not (self.no_target_optimize)))
  176.         if self.target_version:
  177.             lines.append('target_version=%s' % self.target_version)
  178.         
  179.         if self.user_access_control:
  180.             lines.append('user_access_control=%s' % self.user_access_control)
  181.         
  182.         if not self.title:
  183.             pass
  184.         title = self.distribution.get_fullname()
  185.         lines.append('title=%s' % escape(title))
  186.         import time
  187.         import distutils
  188.         build_info = 'Built %s with distutils-%s' % (time.ctime(time.time()), distutils.__version__)
  189.         lines.append('build_info=%s' % build_info)
  190.         return string.join(lines, '\n')
  191.  
  192.     
  193.     def create_exe(self, arcname, fullname, bitmap = None):
  194.         import struct
  195.         self.mkpath(self.dist_dir)
  196.         cfgdata = self.get_inidata()
  197.         installer_name = self.get_installer_filename(fullname)
  198.         self.announce('creating %s' % installer_name)
  199.         if bitmap:
  200.             bitmapdata = open(bitmap, 'rb').read()
  201.             bitmaplen = len(bitmapdata)
  202.         else:
  203.             bitmaplen = 0
  204.         file = open(installer_name, 'wb')
  205.         file.write(self.get_exe_bytes())
  206.         if bitmap:
  207.             file.write(bitmapdata)
  208.         
  209.         
  210.         try:
  211.             unicode
  212.         except NameError:
  213.             pass
  214.  
  215.         if isinstance(cfgdata, unicode):
  216.             cfgdata = cfgdata.encode('mbcs')
  217.         
  218.         cfgdata = cfgdata + '\x00'
  219.         if self.pre_install_script:
  220.             script_data = open(self.pre_install_script, 'r').read()
  221.             cfgdata = cfgdata + script_data + '\n\x00'
  222.         else:
  223.             cfgdata = cfgdata + '\x00'
  224.         file.write(cfgdata)
  225.         header = struct.pack('<iii', 305419899, len(cfgdata), bitmaplen)
  226.         file.write(header)
  227.         file.write(open(arcname, 'rb').read())
  228.  
  229.     
  230.     def get_installer_filename(self, fullname):
  231.         if self.target_version:
  232.             installer_name = os.path.join(self.dist_dir, '%s.%s-py%s.exe' % (fullname, self.plat_name, self.target_version))
  233.         else:
  234.             installer_name = os.path.join(self.dist_dir, '%s.%s.exe' % (fullname, self.plat_name))
  235.         return installer_name
  236.  
  237.     
  238.     def get_exe_bytes(self):
  239.         get_build_version = get_build_version
  240.         import distutils.msvccompiler
  241.         cur_version = get_python_version()
  242.         if self.target_version and self.target_version != cur_version:
  243.             if self.target_version > cur_version:
  244.                 bv = get_build_version()
  245.             elif self.target_version < '2.4':
  246.                 bv = 6
  247.             else:
  248.                 bv = 7.1
  249.         else:
  250.             bv = get_build_version()
  251.         directory = os.path.dirname(__file__)
  252.         if self.plat_name != 'win32' and self.plat_name[:3] == 'win':
  253.             sfix = self.plat_name[3:]
  254.         else:
  255.             sfix = ''
  256.         filename = os.path.join(directory, 'wininst-%.1f%s.exe' % (bv, sfix))
  257.         
  258.         try:
  259.             return open(filename, 'rb').read()
  260.         except IOError:
  261.             msg = None
  262.             raise DistutilsFileError, str(msg) + ', please install the python%s-dev package' % sys.version[:3]
  263.  
  264.  
  265.  
  266.